package me.flyray.bsin.server.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.flyray.bsin.constants.ResponseCode;
import me.flyray.bsin.context.BsinServiceContext;
import me.flyray.bsin.domain.entity.AgentCard;
import me.flyray.bsin.domain.entity.EmbeddingModel;
import me.flyray.bsin.domain.entity.PromptTemplate;
import me.flyray.bsin.exception.BusinessException;
import me.flyray.bsin.facade.service.EmbeddingModelService;
import me.flyray.bsin.infrastructure.mapper.EmbeddingModelMapper;
import me.flyray.bsin.mybatis.utils.Pagination;
import me.flyray.bsin.security.contex.LoginInfoContextHelper;
import me.flyray.bsin.security.domain.LoginUser;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.client.apache.dubbo.annotation.ShenyuDubboService;
import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
import org.apache.shenyu.client.apidocs.annotations.ApiModule;
import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static me.flyray.bsin.constants.ResponseCode.*;

@Slf4j
@ShenyuDubboService(path = "/embeddingModel", timeout = 6000)
@ApiModule(value = "embeddingModel")
public class EmbeddingModelServiceImpl implements EmbeddingModelService {

    @Autowired
    private EmbeddingModelMapper embeddingModelMapper;

    @ApiDoc(desc = "add")
    @ShenyuDubboClient("/add")
    @Override
    public EmbeddingModel add(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        EmbeddingModel embeddingModel = BsinServiceContext.getReqBodyDto(EmbeddingModel.class, requestMap);
        Optional.ofNullable(embeddingModel.getTenantId())
                .or(() -> Optional.ofNullable(loginUser.getTenantId()))
                .ifPresentOrElse(
                        embeddingModel::setTenantId,
                        () -> { throw new BusinessException(ResponseCode.TENANT_ID_NOT_ISNULL); }
                );
        embeddingModelMapper.insert(embeddingModel);
        return embeddingModel;
    }

    @ApiDoc(desc = "delete")
    @ShenyuDubboClient("/delete")
    @Override
    public void delete(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        if (embeddingModelMapper.deleteById(serialNo) == 0){
            throw new BusinessException(ID_NOT_ISNULL);
        }
    }

    @ApiDoc(desc = "edit")
    @ShenyuDubboClient("/edit")
    @Override
    public void edit(Map<String, Object> requestMap) {
        EmbeddingModel embeddingModel = BsinServiceContext.getReqBodyDto(EmbeddingModel.class, requestMap);
        if (embeddingModelMapper.updateById(embeddingModel) == 0){
            throw new BusinessException(ID_NOT_ISNULL);
        }
    }

    @ApiDoc(desc = "getDetail")
    @ShenyuDubboClient("/getDetail")
    @Override
    public EmbeddingModel getDetail(Map<String, Object> requestMap) {
        String serialNo = MapUtils.getString(requestMap, "serialNo");
        EmbeddingModel embeddingModel = embeddingModelMapper.selectById(serialNo);
        if (embeddingModel == null) {
            throw new BusinessException(CONDITION_NOT_EXISTS);
        }
        return embeddingModel;
    }

    @ApiDoc(desc = "getPageList")
    @ShenyuDubboClient("/getPageList")
    @Override
    public IPage<EmbeddingModel> getPageList(Map<String, Object> requestMap) {
        LoginUser loginUser = LoginInfoContextHelper.getLoginUser();
        Object paginationObj =  requestMap.get("pagination");
        Pagination pagination = new Pagination();
        BeanUtil.copyProperties(paginationObj,pagination);

        Page<EmbeddingModel> page = new Page<>(pagination.getPageNum(),pagination.getPageSize());
        LambdaQueryWrapper<EmbeddingModel> warapper = new LambdaQueryWrapper<>();
        warapper.orderByDesc(EmbeddingModel::getCreateTime);
        warapper.eq(EmbeddingModel::getTenantId, loginUser.getTenantId());
        warapper.eq(StringUtils.isNotBlank(loginUser.getMerchantNo()), EmbeddingModel::getMerchantNo, loginUser.getMerchantNo());
        IPage<EmbeddingModel> pageList = embeddingModelMapper.selectPage(page,warapper);
        return pageList;
    }

    @ApiDoc(desc = "getList")
    @ShenyuDubboClient("/getList")
    @Override
    public List<EmbeddingModel> getList(Map<String, Object> requestMap) {
        EmbeddingModel embeddingModel = BsinServiceContext.getReqBodyDto(EmbeddingModel.class, requestMap);
        String tenantId = (String) requestMap.get("tenantId");
        LambdaQueryWrapper<EmbeddingModel> warapper = new LambdaQueryWrapper<>();
        warapper.eq(EmbeddingModel::getTenantId, tenantId);
        List<EmbeddingModel> embeddingModelList = embeddingModelMapper.selectList(warapper);
        return embeddingModelList;
    }

    @ApiDoc(desc = "getDefault")
    @ShenyuDubboClient("/getDefault")
    @Override
    public EmbeddingModel getDefault(Map<String, Object> requestMap) {
        return null;
    }

    @ApiDoc(desc = "setDefault")
    @ShenyuDubboClient("/setDefault")
    @Override
    public void setDefault(Map<String, Object> requestMap) {

    }
}
